package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.ChargeService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.enums.Response;


/**
 * 充电信息控制
 * @author xielf
 *
 */
@RestController
@RequestMapping("/charge")
@Scope(value="prototype")
public class ChargeController {

	/** 日志 */
	private static final Logger LOG = Logger.getLogger("ChargeController:");
	
	@Resource
	private ChargeService chargeService;
	
	/**
	 * 开启充电
	 * @param userCard
	 * @param chargerId
	 * @param chargePlugNo 插头号
	 * @return
	 */
	@RequestMapping(value="/openCharge",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String startCharge(
			@RequestParam String userCard,
			@RequestParam(value="chargerId") String chargerId,
			@RequestParam(value="chargerPlugNo") String chargerPlugNo){
		
		ResultMappingEntity resultMappingEntity= null;
		LOG.info("openCharge ================================="+userCard+"    "+chargerId);
		
		/*StackTraceElement[] ss = Thread.currentThread().getStackTrace();
		for(StackTraceElement s : ss)
			LOG.info(s);*/

		try {
			//开启充电
			JSONObject resultJsonObject = chargeService.startCharge(userCard, chargerId, chargerPlugNo);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 扫码充电
	 * @param userCard
	 * @param chargerId
	 * @param chargePlugNo 插头号
	 * @return
	 */
	@RequestMapping(value="/scanCharge",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String scanCharge(
			@RequestParam String userCard,
			@RequestParam(value="qrCode") String qrCode,
			@RequestParam(value="chargerPlugNo") String chargerPlugNo){
		
		LOG.info("扫码充电Contr date"+DateUtil.getDateTimeLong()+" qrCode="+qrCode+"chargerPlugNo="+chargerPlugNo);
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			//获取所有的站点信息
			JSONObject resultJsonObject = chargeService.scanCharge(userCard, qrCode, chargerPlugNo);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 关闭充电
	 * @param chargeSn
	 * @return
	 */
	@RequestMapping(value="/closeCharge",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String closeCharge(@RequestParam String chargeSn){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {

			JSONObject resultJsonObject = chargeService.closeCharge(chargeSn);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	/**
	 * 获取充电列表
	 * @param userCard
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value="/chargeList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String chargeList(
			@RequestParam String userCard,
			@RequestParam(value="pageNo", defaultValue="1") Integer pageNo){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			JSONObject resultJsonObject = chargeService.chargeList(userCard, pageNo);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("chargeDetailVo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 充电记录详细
	 * @param reservationSn
	 * @return
	 */
	@RequestMapping(value="/chargeDetail",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String chargeDetail(@RequestParam String chargeSn){
		
		ResultMappingEntity resultMappingEntity= null;
		try {

			JSONObject resultJsonObject = chargeService.chargeDetail(chargeSn);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("chargeDetail"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 正在进行的操作
	 * @param userPayCard
	 * @return
	 */
	@RequestMapping(value="/getOngoingOperation",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getOngoingOperation(@RequestParam String userCard){
		
		ResultMappingEntity resultMappingEntity= null;
		try {

			JSONObject resultJsonObject = chargeService.getOngoingOperation(userCard);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.get("operation"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
		
	}
	public static void main(String[] args) {
		
		int a = 3;
		int b = 4;
		a = a | b;
		b = a ^ b;
		a = a ^ b;
		System.out.println(a);
		System.out.println(b);
	}
}
